package com.map.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.map.model.BuildReq;
import com.map.pojo.GaodeRes;
import com.map.util.ArrayUtil;
import com.map.util.ExcelExportUtils;
import com.map.util.HttpClientUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/gaode")
public class GaodeController {

    /***
      http://localhost:8080/gaode/export.json?query=母婴&cityName=保定
      http://localhost:8080/gaode/export.json?query=母婴&cityName=廊坊
      http://localhost:8080/gaode/export.json?query=母婴&cityName=石家庄
      http://localhost:8080/gaode/export.json?query=母婴&cityName=邢台
     */

    @RequestMapping(value = "/export.json")
    public void export(HttpServletResponse response, String query,String cityName){
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        ServletOutputStream out = null;
        try {
            String url = "https://restapi.amap.com/v3/place/text";
            Map<String, Object> basemap = BuildReq.buildGaodeReq(1, query, cityName);
            String baseResStr = HttpClientUtil.sendGet(url,basemap);
            GaodeRes basePoi = JSON.parseObject(baseResStr, GaodeRes.class);

            List list = new ArrayList();

            for (int t=0;t<basePoi.getCount()/20;t++){
                Thread.sleep(1000);

                Map<String, Object> mapq = BuildReq.buildGaodeReq(t+1, query, cityName);
                String resStr = HttpClientUtil.sendGet(url,mapq);
                GaodeRes poi = JSON.parseObject(resStr, GaodeRes.class);

                for (int i= 0; i<poi.getPois().size();i++){
                    GaodeRes.Pois pois = poi.getPois().get(i);

                    if (!pois.getAdname().contains("区")){
                        continue;
                    }

                    Map map = new HashMap();
                    map.put("addr",pois.getCityname()+pois.getAddress());
                    map.put("name",pois.getName());
                    map.put("type",pois.getType());
                    map.put("tel",pois.getTel());
                    map.put("area_name",pois.getCityname()+pois.getAdname());
                    list.add(map);
                }
            }

            String [][] arr = ArrayUtil.listToArray(list);
            String fileName = new String((cityName+query+"数据"+System.currentTimeMillis()+".xls").getBytes("UTF-8"),"ISO8859-1");
            response.setHeader("content-disposition", "attachment;filename="+fileName);
            out = response.getOutputStream();
            ExcelExportUtils.exportFormatExcel(arr, "母婴店铺数据", out);

        } catch (Exception e) {

            System.out.println("执行出错");
        } finally {
            if (out != null) {
                try {
                    out.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
